﻿

using System;
using System.Data.SqlClient;
using System.Transactions;
using ufida.u9.cust.dz.beplugin.Utils;
using UFSoft.UBF.Util.DataAccess;
using UFSoft.UBF.Util.Log;

namespace ufida.u9.cust.dz.beplugin.loger
{
    public class LogUtil
    {
        public void RecordLog(string docno, string type, object data ,int flag, string msg)
        {
            string sql = $"insert into dz_jkylog(docno,type,jsondata,flag,msg) values('{docno}','{type}','{JsonUtils.Serialize(data)}',{flag},'{msg}')";
            ExecByNewTransaction(sql);
        }
  
        private void ExecByNewTransaction(string sql)
        {

            string connectionString = DataAccessor.GetConn().ConnectionString;

            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
            {

                try
                {
                    // 创建数据库连接
                    using (SqlConnection conn = new SqlConnection(connectionString))
                    {
                        conn.Open();

                        // 创建事务
                        SqlTransaction tran = conn.BeginTransaction();

                        SqlCommand cmd = new SqlCommand(sql, conn, tran);
                        cmd.ExecuteNonQuery();

                        // 提交事务
                        tran.Commit();

                        // 完成事务范围
                        scope.Complete();
                    }
                }
                catch (Exception ex)
                {
                    // 异常处理，如果发生错误则回滚事务
                    scope.Dispose();

                }

            }
        }
    }
}
